/*
 * Fichier:   IhmMission.java
 * 
 * Autheur: Marras	Aurelien
 *
 * 
 */


package com.ardrone.revue;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.ardrone.dao.DatabaseHelper;

public class IhmMission extends Activity implements OnClickListener 
{

	DatabaseHelper db;
	
    Button pilotage = null;
    Button quitter = null;
    Button synchronisation = null;
    private Context context;
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.ihmmission);
        this.context=this;
        db =DatabaseHelper.getInstance(this.getApplicationContext());
        try {
			db.createDataBase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        db.openDataBase();

        pilotage = (Button)this.findViewById(R.id.pilotage);
        quitter = (Button)this.findViewById(R.id.quitter);
        synchronisation = (Button)this.findViewById(R.id.synchronisation);
      

        pilotage.setOnClickListener(this);
        quitter.setOnClickListener(this);
        synchronisation.setOnClickListener(this);
       
        ArrayList<String> array = db.recupererTitreProduits();
        ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,array);
        Spinner sp = (Spinner)findViewById(R.id.spinner);
        sp.setAdapter(adapter);
       final ArrayList<Produit> listeProduits = db.recupererLaListeDesProduits();
      final ListView list = (ListView)findViewById(R.id.listView1);
      sp.setOnItemSelectedListener(new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Log.e("caca",(String)arg0.getItemAtPosition(arg2));
			// TODO Auto-generated method stub
			for(Produit p : listeProduits)
			{
				Log.e(p.getTitre(),(String)arg0.getItemAtPosition(arg2));
				if(p.getTitre().equals((String)arg0.getItemAtPosition(arg2)))
				{
					
					 String[] ListMissions={p.getId()+"",p.getTitre(),p.getCodeBarre(),p.getDescription()};
					ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, ListMissions);    
					list.setAdapter(adapter1);	
					break;
				}
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
	});
        
            
       
			
		
		/*
        Spinner sp = (Spinner)findViewById(R.id.spinner);;
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor, new String[]{ "type"}, new int[]{ android.R.id.text1});
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
	*/
      
     
}


	public void onClick(View v) 
	{
		if(v.getId() == R.id.pilotage)
		{
            Intent i = new Intent(IhmMission.this, IhmVol.class);
             startActivity(i);
		}
		if( v.getId() == R.id.quitter)
		{
			finish();
		}
		if(v.getId() == R.id.synchronisation)
		{

		}
	  
	}
	

}

	
	
	
